package com.example.icampusevent;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.icampusevent.jsonobjects.FriendEvent;
import com.example.icampusevent.jsonobjects.Invitation;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.view.Menu;
import android.widget.Adapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MessageActivity extends Activity {
	
	private String userId = null;
	private ArrayList<Invitation> invitationList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        
        userId = getIntent().getStringExtra("USERID");
        
        int[] colors = {0, 0x575CE800, 0};
        ListView myList = (ListView)findViewById(R.id.message_list);
        myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
        myList.setDividerHeight(2);
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	Intent intent = getIntent();
        this.userId = intent.getStringExtra("USERID");
        
        // Populate invitation list
        String url = "http://hamedaan.usc.edu:8080/team15/AlertServlet?q=all&userid=" + userId;
        LoadInvitationListTask task = new LoadInvitationListTask();
        task.execute(url);
    }
    
    @Override
    public void onNewIntent(Intent newIntent) {
    	setIntent(newIntent);
    }
    
    private class LoadInvitationListTask extends AsyncTask<String, Integer, Reader> {
	     protected Reader doInBackground(String... url) {
	    	 InputStream source = retrieveStream(url[0]);
	 		 if(source == null) return null;
	 		 
	 		 Reader inputReader = new InputStreamReader(source);
	 		 return inputReader;
	     }

	     protected void onPostExecute(Reader json) {
	    	 Type listType = new TypeToken<ArrayList<Invitation>>(){}.getType();
	 		 Gson gson = new Gson();
	 		 ListView listView = (ListView)findViewById(R.id.message_list);
			 invitationList = gson.fromJson(json, listType);
			 Adapter adapter = new MessageListAdapter(MessageActivity.this, invitationList);
		     listView.setAdapter((ListAdapter)adapter);
	     }
	     
	    // Get data stream from server
	 	private InputStream retrieveStream(String url) {
	 		DefaultHttpClient client = new DefaultHttpClient();
	 		HttpGet request = new HttpGet(url);
	 		try {
	 			HttpResponse response = client.execute(request);
	 			final int status = response.getStatusLine().getStatusCode();
	 			if(status != HttpStatus.SC_OK) {
	 				toast("ERROR CODE: " + status + " for URL " + url);
	 				return null;
	 			}
	 			HttpEntity responseEntity = response.getEntity();
	 			return responseEntity.getContent();
	 		}
	 		catch(IOException e) {
	 			request.abort();
	 			toast("ERROR for " + url);
	 		}
	 		
	 		return null;
	 	}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_message, menu);
        return true;
    }
    
    
    public String getUserId() {
    	return this.userId;
    }
    
    
    public void toast(String s) {
		CharSequence text = s;
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(this, text, duration);
		toast.show();
	}
}
